import processing.core.PApplet;
import processing.core.PImage;

  public class Button {
    
    private mainInterface gui;
    private PApplet parent;
    private Configuration config;
    private String text;
    private PImage up, over, down;
    private boolean isToggle;
    private float X, Y, width, height;
    private int display; // -1 = dont display, 0 = display up, 1 = display over, 2 = display down
    private static final int HIDDEN = -1, UP = 0, OVER = 1, DOWN = 2;
    private int action;
    
    public Button(mainInterface _gui, Configuration _config, PImage _up, PImage _over, PImage _down, float _x, float _y, int method, boolean isT) {
      
      gui = _gui;
      parent = gui.parent;
      config = _config;
      up = _up;
      over = _over;
      down = _down;
      display = -1; //dont display
      X = _x;
      Y = _y;
      width = _up.width;
      height = _up.height;
      action = method;
      isToggle = isT;
      text = null;
    }
    
public Button(mainInterface _gui, Configuration _config, PImage _up, PImage _over, PImage _down, float _x, float _y, int method, boolean isT, String _text) {
      
      new Button(_gui,_config,_up,_over,_down,_x,_y,method,isT);
      text = _text;
    }
    
    public void draw() {
      
      switch(display) {
        case(HIDDEN):
          //dont display
          break;
        case(UP):
          parent.image(up, X, Y);
          break;
        case(OVER):
          parent.image(over, X, Y);
          break;
        case(DOWN):
          parent.image(down, X, Y);
          break;
        default:
          break;
      }
      
      if (text != null) {
        parent.text(text, X + (width / 2) - (text.length() * 2), Y + (height / 2) );
      }
      
    }
    
    public void setConfig(Configuration cfg) {
      config = cfg;
    }
    
    public void setDisplay(int i) {
      display = i;
    }
    
    public void over() {
      display = 1;
    }
    
    public boolean withinBounds(float x, float y) {
      if(x >= X && x <= X + width && y >= Y && y <= Y + height && display >= 0) {
        return true;
      }else {
        return false;
      }
    }
        
    public void press() {
      
      if (isToggle ) {
        //do nothing
      }
      
      switch(action) {
        
        //menu actions
        case(00):
          //this one is executed in the release rather than the press
          break;
        case(01):
          //this one is executed in the release rather than the press
          break;
        case(02):
          //this one is executed in the release rather than the press
          break;
        case(03):
          //this one is executed in the release rather than the press
          break;
        
        //session configuration view
        case(10):
          display = 2;
          break;
        case(11):
          display = 2;
          break;
        case(12):
          display = 2;
          break;
        case(13):
          display = 2;
          break;
        default:
          break;
      }
    }
    
    public void release() {
      
      if (isToggle && (display == 0 || display == 1)) {
        display = 2;
      } else if (isToggle && display == 2){
        display = 0;
      } 
      
      switch(action) {
        
        //menu actions
        case(00):
          config.setMode(0); //drawmode
          gui.drawingView();
          break;
        case(01):
          config.setMode(1); //textmode
          gui.textView();
          break;
        case(02):
          config.setMode(2); //actionsmode
          gui.actionsView();
          break;
        case(03):
          ((thetablet)parent).Export(); //savemode
          break;
          
        //session configuration actions
        case(10):
          display = 0;
          ((thetablet)parent).createPage();                //new session
          gui.sessionConfigurationView = gui.DISPLAYIP;
          break;
        case(11):
          display = 0;
          gui.sessionConfigurationView = gui.ENTERIP;        //join session
          break;
        case(12):
          display = 0;
          ((thetablet)parent).joinPage(gui.ipEntry);     //confirm ip and join
          break;
        case(13):
          display = 0; 
          gui.sessionConfigurationView = gui.HIDDEN;
          gui.drawingView();                                 //start session
          break;
        case(14):
          display = 0;
        gui.sessionConfigurationView = gui.WELCOME;
          
        default:
          break;
    }
        
   }
  }